package com.hzeng.util;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.RowSorter;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

public class ProgramDialog {
	JDialog dialog = null;
	JTable allProgramTable = null;
	JScrollPane allScrollPane = null;
	JScrollPane destScrollPane = null;
	JTable destProgramTable = null;
	JPanel dialogJPanel = null;
	JButton addPMButton = null;
	JButton deletePMButton = null;
	JButton saveButton = null;
	JButton quitButton = null;
	
	JFrame container = null;

	public ProgramDialog(JFrame container) {
		dialog = new JDialog(container,"progress list",true);
		dialogJPanel = new JPanel();
		dialogJPanel.setLayout(new GridBagLayout());
		dialog.setContentPane(dialogJPanel);
		
		GridBagConstraints constraints = new GridBagConstraints();
		constraints.fill = GridBagConstraints.NONE;
		constraints.anchor = GridBagConstraints.CENTER;
		constraints.weightx = 4;
		constraints.weighty = 5;
		
		
		initListPanel();
		initButtons();
		
		add(allScrollPane,constraints,0,0,2,4);
		add(addPMButton,constraints,2,1,1,1);
		add(deletePMButton,constraints,2,2,1,1);
		add(destScrollPane,constraints,3,0,1,4);
		
		add(quitButton,constraints,1,4,1,1);
		add(saveButton,constraints,2,4,1,1);
		
		
		dialog.setResizable(false);
		dialog.setLocation(300, 250);
		dialog.setPreferredSize(new Dimension(600, 400));
		dialog.setSize(new Dimension(600, 400));
		dialog.pack();
		dialog.setVisible(true);
	//	dialog.setBounds(100,100,400,340);
		dialog.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
	}

	public void add(Component c,GridBagConstraints constraints, int x,int y,int w,int h) {
		constraints.gridx = x;
		constraints.gridy = y;
		constraints.gridwidth = w;
		constraints.gridheight = h;
		
		dialogJPanel.add(c,constraints);
	}
	
	public void initButtons() {
		addPMButton = new JButton(">>");
		addPMButton.setPreferredSize(new Dimension(50, 30));
		addPMButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				ProgramTableModel tableModel = (ProgramTableModel) allProgramTable.getModel();
				int[] selectRowsIndex = allProgramTable.getSelectedRows();
				
				if (selectRowsIndex == null || selectRowsIndex.length < 1) {
					JOptionPane.showMessageDialog(null, "warning:there is no program selected in left table!");
					return;
				}
				
				ProgramTableModel destTableModel = (ProgramTableModel) destProgramTable.getModel();
				
				for (int i = 0; i < selectRowsIndex.length; i++) {
					int convertRowIndex = allProgramTable.convertRowIndexToModel(selectRowsIndex[i]);
					Program p = tableModel.getRow(convertRowIndex);
					destTableModel.addRow(p);
					
				}
				
				for (int i = 0; i < selectRowsIndex.length; i++) {
					int convertRowIndex = allProgramTable.convertRowIndexToModel(selectRowsIndex[i]);
					tableModel.removeRow(convertRowIndex);
					
				}
				destProgramTable.setModel(destTableModel);
				destProgramTable.updateUI();
				
				allProgramTable.setModel(tableModel);
				allProgramTable.updateUI();
			}
		});
		deletePMButton = new JButton("<<");
		deletePMButton.setPreferredSize(new Dimension(50, 30));
		deletePMButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				ProgramTableModel destTableModel = (ProgramTableModel) destProgramTable.getModel();
				int[] selectRowsIndex = destProgramTable.getSelectedRows();
				
				if (selectRowsIndex == null || selectRowsIndex.length < 1) {
					JOptionPane.showMessageDialog(null, "warning:there is no program selected in right table!");
					return;
				}
				
				ProgramTableModel allTableModel = (ProgramTableModel) allProgramTable.getModel();
				
				for (int i = 0; i < selectRowsIndex.length; i++) {
					int convertRowIndex = destProgramTable.convertRowIndexToModel(selectRowsIndex[i]);
					Program p = destTableModel.getRow(convertRowIndex);
					allTableModel.addRow(p);
					
				}
				for (int i = 0; i < selectRowsIndex.length; i++) {
					int convertRowIndex = destProgramTable.convertRowIndexToModel(selectRowsIndex[i]);
					destTableModel.removeRow(convertRowIndex);
				}
				allProgramTable.setModel(allTableModel);
				allProgramTable.updateUI();
				
				destProgramTable.setModel(destTableModel);
				destProgramTable.updateUI();
				
			}
		});
		
		quitButton = new JButton("quit");
		quitButton.setPreferredSize(new Dimension(80, 30));
		quitButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				dialog.dispose(); 
			}
		});

		saveButton = new JButton("save");
		saveButton.setPreferredSize(new Dimension(80, 30));
		saveButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				ProgramTableModel destTableModel = (ProgramTableModel) destProgramTable.getModel();
				List<Program> targetPrograms = destTableModel.getProgramList();
				
				if (targetPrograms == null || targetPrograms.size() < 1) {
					
				}else {
					//set program list to List Panel in main UI
					ProgramMonitorUI mainUi = ProgramMonitorUI.getInstance();
					JTable destTable = mainUi.getDestProgramTable();
					ProgramTableModel tableModel = (ProgramTableModel)destTable.getModel();
					tableModel.setProgramList(targetPrograms);
					
					//set program list to queue
					ArrayList<Long> pids = new ArrayList<Long>();
					
					for (int i = targetPrograms.size() - 1; i >= 0; i--) {
						pids.add(targetPrograms.get(i).getPid());
					}
					mainUi.getMyQueue().setPids(pids);
					destTable.updateUI();
				}
				
				
				dialog.dispose(); 
			}
		});
	}
	
	public void initListPanel() {
		ProgramMonitor pm = ProgramMonitor.getInstance();
		    
	    List<Program> programs = pm.getAllProcessList();
	    ProgramTableModel tableModel = new ProgramTableModel();
	    tableModel.setProgramList(programs);
	    tableModel.setColumnCount(3);
	    
	//    tableModel.setDataVector(tableData, tableHeaders);
	    allProgramTable = new JTable(tableModel);
	    
	    RowSorter<javax.swing.table.TableModel> sorter = new TableRowSorter<TableModel>(allProgramTable.getModel());
	    allProgramTable.setRowSorter(sorter);
	    
	    allScrollPane = new JScrollPane(allProgramTable);
	    allScrollPane.setPreferredSize(new Dimension(350, 300));
	    allScrollPane.setSize(new Dimension(350, 300));
	    
	    
	    
	    ProgramTableModel destTableModel = new ProgramTableModel();
	    destTableModel.setColumnCount(2);
	    
	    destProgramTable = new JTable();
	    destProgramTable.setModel(destTableModel);
	    RowSorter<javax.swing.table.TableModel> sorter2 = new TableRowSorter<TableModel>(destProgramTable.getModel());
	    destProgramTable.setRowSorter(sorter2);
	    
	    destProgramTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
	    
	 //   destProgramTable.setBorder(BorderFactory.createTitledBorder("All running progress:"));
		    
	    destScrollPane = new JScrollPane(destProgramTable);
	    destScrollPane.setPreferredSize(new Dimension(150, 300));
	    destScrollPane.setSize(new Dimension(150, 300));
	//    destScrollPane.setViewportView(destProgramTable);
	}
	
	public static void main(String[] args) {
		
	}

}